package cn.gwm.flink.streaming.strategy.vehiclepredict;

import cn.gwm.flink.streaming.constant.ChargePredictFields;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.state.MapState;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * @author : GW00280745
 * @date : 2023/5/4 8:17
 * @description :
 *
 *

 */
public class OneSocCal implements Serializable {
    public String process(JSONObject line, MapState<String, String> oneSocTimeState,MapState<String, String> latestEstimateState){
        String predict = oneSocCal(line, oneSocTimeState);
        if (StringUtils.isBlank(predict)) {
            return null;
        }
        try {
            //配合各种state
            String[] split = predict.split(ChargePredictFields.ins().char3);
            latestEstimateState.put(ChargePredictFields.ins().stdFieldEstimateTime, split[0]);
            line.put(ChargePredictFields.ins().stdFieldEstimateTime, split[0]);

            //predict_time字段   原本格式= 2022-09-10 17:05:22  临时用它存储每次预测的过程
            String predictTime = predictTime(line, split[0]);
            latestEstimateState.put(ChargePredictFields.ins().stdFieldPredictTime, predictTime);
            line.put(ChargePredictFields.ins().stdFieldPredictTime, predictTime);

            String chargeGroup = line.getString(ChargePredictFields.ins().dwsFieldChargeBegin);
            latestEstimateState.put(chargeGroup, predict);
            line.put(ChargePredictFields.ins().stdFieldPredictTimeV2, predict);
            //智能充电模块-数据对接 格式  charge:status:{vin}:{startChargeTime}
//            intellectCharge(line, socIntervalJedis, predict)
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    private String oneSocCal(JSONObject line, MapState<String, String> oneSocTimeState){
        try {
            String vinVal = line.getString(ChargePredictFields.ins().srcFieldVin);
            String startChargeTime = line.getString(ChargePredictFields.ins().srcFieldStartChargeTime);
            String chargeId = vinVal.concat(startChargeTime);
            String s = oneSocTimeState.get(chargeId);
            String[] split = s.split(ChargePredictFields.ins().char3);
            if (split==null || split.length<3){
                return null;
            }
            String deltaTime = split[2];
            float v = Float.parseFloat(deltaTime);
            Float bmsSoc = line.getFloat(ChargePredictFields.ins().srcFieldBmsSoc);
            float totalTime = (100 - bmsSoc) * v;
            if (totalTime==0){
                System.out.println(String.format("vin=%s , startchargetime = %s , state_val=%s ",vinVal,startChargeTime,s));
            }
            String concat = String.valueOf((int) totalTime).concat(ChargePredictFields.ins().char3)
                    .concat(bmsSoc.toString()).concat(ChargePredictFields.ins().char3)
                    .concat("100");
            line.put("flag","middle");
            return concat;
        }catch (Exception e){
            e.printStackTrace();
        }
        return defaultCal(line);
    }
    private String defaultCal(JSONObject line){
        Float bmsSoc = line.getFloat(ChargePredictFields.ins().srcFieldBmsSoc);
        Float bmsCurr = line.getFloat(ChargePredictFields.ins().srcFieldBmsBatterCurrRm);
        float totalTime = 0;
        if (true){
            if (Math.abs(bmsCurr)<DwsStrConstant.ins().num7){
                totalTime = (100-(bmsSoc+1))*12f;
            }else if (Math.abs(bmsCurr)< DwsStrConstant.ins().num20){
                totalTime = (100-bmsSoc)*6.2f;
            }
            if (totalTime<=0){
                totalTime = 15;
            }
        }
//        totalTime = (100-bmsSoc) * 6.2f
//        totalTime = -999999
        String concat = String.valueOf((int) totalTime).concat(ChargePredictFields.ins().char3).concat(bmsSoc.toString()).concat(ChargePredictFields.ins().char3).concat("100");
        line.put("flag","default");
        return concat;
    }
    private String predictTime(JSONObject line, String deltaTime) {
        try {
            //预测时间单位=min
            long estiTime = new BigDecimal(deltaTime).longValue();
            long l = estiTime * 60 * 1000L;
            Long tid = line.getLong(ChargePredictFields.ins().srcFieldTid);
            long ls = tid + l;
            String format = DateFormatUtils.format(ls, ChargePredictFields.ins().yyyyMmddss1);
            return format;
        }catch (Exception e){
            return ChargePredictFields.ins().blankStr9;
        }
    }
}
